package cn.gene.timeyan.security.sso.utils;

import cn.gene.timeyan.security.sso.config.MailPropertiesConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 邮件发送类
 */
@Component
public class EmailUtils {
    @Autowired
    private  JavaMailSender javaMailSender;
    @Autowired
    private MailPropertiesConfig config;

    /**
     * 发送验证码
     * @param email
     * @param verificationCode
     * @throws Exception
     */
    public void sendVerificationCode(String email,String verificationCode) throws Exception{
        //创建简易邮件消息对象
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //填写发送人邮箱
        mailMessage.setFrom(config.getUsername());
        //接受者邮箱
        mailMessage.setTo(email);
        //邮件标题
        mailMessage.setSubject("时光源邮箱验证（系统自动邮件，请勿回复）");
        //邮件内容
//        String  context=new String(.getBytes("utf-8"));
        mailMessage.setText("您的邮箱验证码: "+verificationCode+"，请勿泄漏给他人。\n本邮件由系统自动发出，请勿回复。");
        //发送时间
        mailMessage.setSentDate(new Date());
        javaMailSender.send(mailMessage);
    }

}
